<?php
// +----------------------------------------------------------------------
// | 随访系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2050 http://www.hedesoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小李子 < 490959289@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Validate;
class Attribute extends Base
{
	//属性列表
    public function index()
    {
    	$where = array();
    	$where['is_del'] = 0;
    	$q = $this->request->param('q');
    	if ($q){
    		 $where['attr_theme|attr_label|attr_name'] = array('LIKE', '%'.$q.'%');
    	}
    	$list = model('Attribute')->where($where)->order('ctime desc ,attr_sort asc')->paginate(15);
    	$page = $list->render();
    	$this->assign('list', $list);
    	$this->assign('page', $page);
    	return $this->fetch();
    }
    //添加属性
    public function attr_form()
    {
    	$attr_id = $this->request->param('attr_id');
    	$attr = model('Attribute')->get($attr_id);
    	$this->assign('attr', $attr);
    	return $this->fetch();
    }
    //删除属性
    public function delAttr()
    {
    	$data['attr_id'] = $this->request->param('attr_id');
    	$data['is_del'] = 1;
    	$attr_status = model('Attribute')->update($data);
    	if ($attr_status){
    		$this->create_adminlog('删除属性ID：'.$data['attr_id'].'');
    		echo json_encode(['status'=>1,'msg'=>'删除成功']);exit();
    	}else{
    		echo json_encode(['status'=>0,'msg'=>'删除失败']);exit();
    	}
    }
    //保存属性
    public function saveAttr()
    {
    	$rule = [
	    'attr_theme'  => 'require|max:255',
	    'attr_label'  => 'require|max:255',
	    'attr_name'  => 'require|max:255',
	    'attr_sort'  => 'number|between:0,127',
		];

		$msg = [
		    'attr_theme.require' => '属性主题不能为空',
		    'attr_theme.max' => '属性主题长度不能超过255个字符',
		    'attr_label.require' => '属性标签不能为空',
		    'attr_label.max' => '属性标签长度不能超过255个字符',
		    'attr_name.require' => '属性名称不能为空',
		    'attr_name.max' => '属性名称长度不能超过255个字符',
		    'attr_sort' => '属性排序请输入0-127之间',
		];

    	$data['attr_theme'] = $this->request->param('attr_theme');
    	$data['attr_label'] = $this->request->param('attr_label');
    	$data['attr_name'] = $this->request->param('attr_name');
    	$data['attr_value'] = $this->request->param('attr_value');
    	$data['attr_sort'] = $this->request->param('attr_sort');
    	$data['ctime'] = time();
    	$validate = new Validate($rule, $msg);
    	if (!$validate->check($data)){
    		echo json_encode(['status'=>0,'msg'=>$validate->getError()]);exit();
    	}
    	$attr_id = $this->request->param('attr_id');
    	if ($attr_id){
    		$attr_status = model('Attribute')->where('attr_id',$attr_id)->update($data);
    	}else{
    		$attr_status = model('Attribute')->insertGetId($data);
    	}
    	if ($attr_status){
    		$this->create_adminlog('保存 '.$data['attr_theme'].' >'.$data['attr_name'].' 属性');
    		echo json_encode(['status'=>1,'msg'=>'保存成功']);exit();
    	}else{
    		echo json_encode(['status'=>0,'msg'=>'保存失败']);exit();
    	}
    }
}
